import { Component } from "react";
import { withRouter } from 'react-router-dom';

// connect
import { connect } from "react-redux";
import { bindActionCreators } from "redux";
//action
import { setTokenAction, setRefreshTokenAction, setUsernameAction, accountLoginAction } from "@/stroe/action/App";


// test
import cookies from "react-cookies";

class DingTalkLogin extends Component{
    constructor() {
        super()
        this.state = {

        }
    }

    componentDidMount() {
        const data = this.props.location.search
        const data1 = data.split('?')[1]
        const data2 = data1.split('&')
        const username = data2[0].split('=')[1]
        const access_token = data2[1].split('=')[1]
        const refresh_token = data2[2].split('=')[1]
        const department = data2[3].split('=')[1]
        const avatar = data2[4].split('=')[1]
        cookies.save('access_token', access_token, { path: '/' });
        cookies.save('refresh_token', refresh_token, { path: '/' });
        cookies.save('username', username, { path: '/' });
        cookies.save('department', department, { path: '/' });
        cookies.save('avatar', avatar, { path: '/'});
        this.props.history.push('/index/home/dashboard');
    }

    render() {
        return '钉钉扫码登录成功'
    }

}


const mapDispatchToProps = (dispatch) => {
    return {
        actions: bindActionCreators({
            setToken: setTokenAction,
            setRefreshToken: setRefreshTokenAction,
            setUsername: setUsernameAction,
            handlerLogin: accountLoginAction
        }, dispatch)
    }
}

export default connect(
    null,
    mapDispatchToProps
)(withRouter(DingTalkLogin));
